import java.io.*;
import java.net.*;

public class WazzupClient {
    public static void main(String[] args) throws IOException {
        String hostName;
        int portNumber;

        if (args.length != 2) {
            System.err.println("Usage: java WazzupClient <host name> <port number>");
            System.exit(1);
        }

        hostName = args[0];
        portNumber = Integer.parseInt(args[1]);

        try ( Socket echoSocket = new Socket(hostName, portNumber);
              PrintWriter out = new PrintWriter(echoSocket.getOutputStream(), true);
              BufferedReader in = new BufferedReader(new InputStreamReader(echoSocket.getInputStream()))) {
            String serverResponse;

            out.println("Wazzup!");
            while ( (serverResponse = in.readLine() ) != null) {
                System.out.println(serverResponse);
            }
        } catch (UnknownHostException e) {
            System.err.println("Don't know about host " + hostName);
            System.exit(1);
        } catch (IllegalArgumentException e) {
            System.err.println("Port number is invalid " + portNumber);
            System.exit(1);
        }
        catch (IOException e) {
            System.err.println("Couldn't get I/O for the connection to " + hostName);
            System.err.println(e.getMessage());
            System.exit(1);
        }
    }
}
